package com.nantai.jiafang.nantaijiafang;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.nantai.jiafang.nantaijiafang.nt5s.app.PickerApplication;

import org.xutils.DbManager;
import org.xutils.x;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by wuning on 2016/8/10.
 */
public class MyApplication extends PickerApplication {
    public static DbManager.DaoConfig daoConfig;

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化xutils;
        initUtils();
        //初始化数据库
        initDataBase();
        //初始化极光推送
        //initJPush();
//        if (LeakCanary.isInAnalyzerProcess(this)){
//            return;
//        }
//        LeakCanary.install(this);

    }

    private void initJPush() {
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }

    private void initDataBase() {
        daoConfig = new DbManager.DaoConfig()
                .setDbName("NTJF.db")
                // 不设置dbDir时, 默认存储在app的私有目录.
//                .setDbDir(new File("/sdcard/Android/data/com.nantai.jiafang.nantaijiafang/files")) // "sdcard"的写法并非最佳实践, 这里为了简单, 先这样写了.
                .setDbVersion(6)
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        // 开启WAL, 对写入加速提升巨大
                        db.getDatabase().enableWriteAheadLogging();
                    }
                })
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                        //: ...

//                        String save = SjieTools.getSharedValue(MyApplication.this, IConstant.FILE_USER, IConstant.SP_ISPWD_REMEMBER);
//                        if (save.equals("1")) {
//                            try {
//                                db.update(ProductionOrderInfo.class,
//                                        String.valueOf(new KeyValue("userId", SjieTools.getSharedValue(MyApplication.this, IConstant.FILE_USER, IConstant.SP_LOGIN_NAME))));
//                                db.update(FabricBatchInfo.class,
//                                        String.valueOf(new KeyValue("userId", SjieTools.getSharedValue(MyApplication.this, IConstant.FILE_USER, IConstant.SP_LOGIN_NAME))));
//                            } catch (DbException e) {
//                                e.printStackTrace();
//                            }
//
//
//                        }

                        //db.addColumn("Timestamp");
                        // db.dropTable(...);
                        // ...
                        // or
                        // db.dropDb();
                    }
                });
    }

    private boolean isDebug = true;

    private void initUtils() {
        x.Ext.init(this);
        x.Ext.setDebug(false); // 是否输出debug日志, 开启debug会影响性能.
    }
}
